package com.my12306.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my12306.common.constant.CommonConstants;
import com.my12306.common.model.vo.PageVo;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.SnowflakeIdWorker;
import com.my12306.common.util.response.ResponseData;
import com.my12306.train.dao.MarshallingDao;
import com.my12306.train.dao.TrainDao;
import com.my12306.train.model.entity.MarshallingEntity;
import com.my12306.train.model.entity.TrainEntity;
import com.my12306.train.model.vo.TrainVo;
import com.my12306.train.service.TrainService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("trainService")
public class TrainServiceImpl extends ServiceImpl<TrainDao, TrainEntity> implements TrainService {

    @Resource
    MarshallingDao marshallingDao;

    @Override
    public PageUtils queryTrainPage(PageVo pageVo) {
        pageVo = pageVo.init();
        List<TrainEntity> trains = this.baseMapper.queryTrainPage(pageVo, CommonConstants.LogicDelete.no_delet.getValue());
        if (trains != null) {
            Integer dataCount = queryTrainCount(pageVo, CommonConstants.LogicDelete.no_delet.getValue());
            System.out.println("数据总量：" + dataCount);
            return PageUtils.build(pageVo, trains, dataCount);
        }
        return null;
    }

    @Override
    public Integer queryTrainCount(PageVo pageVo, int logicDelete) {
        Integer count = this.baseMapper.queryTrainCount(pageVo, logicDelete);
        return count;
    }

    @Override
    public Long saveTrain(TrainEntity train) {
        if (train != null) {
            train.setTrainId(SnowflakeIdWorker.generateId());
            train.setCreateDate(new Date());
            train.setUpdateDate(new Date());
            int insert = this.baseMapper.insert(train);
            System.out.println("成功添加列车数："+ insert);

            QueryWrapper<MarshallingEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("logic_delete",CommonConstants.LogicDelete.no_delet.getValue());
            queryWrapper.eq("group_id",train.getGroupId());
            MarshallingEntity marshallingEntity = marshallingDao.selectOne(queryWrapper);
            if (marshallingEntity != null){
                System.out.println(marshallingEntity);
                marshallingEntity.setMark(String.valueOf(1));
                marshallingEntity.setUpdateDate(new Date());
                marshallingDao.updateById(marshallingEntity);
            }
        }
        return null;
    }

    @Override
    public void updateTrainById(TrainVo train) {
        if (train != null) {
            TrainEntity trainEntity = new TrainEntity();
            BeanUtils.copyProperties(train, trainEntity);
            trainEntity.setUpdateDate(new Date());
            this.baseMapper.updateById(trainEntity);
            QueryWrapper<MarshallingEntity> queryWrapper = new QueryWrapper<>();
            QueryWrapper<MarshallingEntity> queryWrapper1 = new QueryWrapper<>();
            queryWrapper.eq("logic_delete",CommonConstants.LogicDelete.no_delet.getValue());
            queryWrapper.eq("group_id",train.getGroupId());
            queryWrapper1.eq("logic_delete",CommonConstants.LogicDelete.no_delet.getValue());
            queryWrapper1.eq("group_id",train.getOriginGroupId());
            MarshallingEntity marshallingEntity = marshallingDao.selectOne(queryWrapper);
            MarshallingEntity marshallingEntity1 = marshallingDao.selectOne(queryWrapper1);
            if (marshallingEntity != null){
                System.out.println(marshallingEntity);
                marshallingEntity.setMark(String.valueOf(1));
                marshallingEntity.setUpdateDate(new Date());
                marshallingDao.updateById(marshallingEntity);
            }
            if (marshallingEntity1 != null){
                System.out.println(marshallingEntity1);
                marshallingEntity1.setMark(String.valueOf(0));
                marshallingEntity1.setUpdateDate(new Date());
                marshallingDao.updateById(marshallingEntity1);
            }
        }
    }

    @Override
    public TrainEntity queryTrainByTrainId(Long trainId) {
        QueryWrapper<TrainEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("logic_delete",CommonConstants.LogicDelete.no_delet.getValue());
        queryWrapper.eq("train_id",trainId);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional
    public ResponseData deleteTrainBatchByIds(List<Long> trainIds) {
        trainIds.stream().forEach(trainId->{
            TrainEntity train = this.baseMapper.selectById(trainId);
            QueryWrapper<MarshallingEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("logic_delete",CommonConstants.LogicDelete.no_delet.getValue());
            queryWrapper.eq("group_id",train.getGroupId());
            MarshallingEntity marshallingEntity = marshallingDao.selectOne(queryWrapper);
            if (marshallingEntity != null){
                System.out.println(marshallingEntity);
                marshallingEntity.setMark(String.valueOf(0));
                marshallingEntity.setUpdateDate(new Date());
                marshallingDao.updateById(marshallingEntity);
            }
        });
        //删除自身
        this.baseMapper.deleteTrainBatchByIds(trainIds, CommonConstants.LogicDelete.delete.getValue());
        return ResponseData.ok();
    }

    @Override
    public List<TrainEntity> queryAllTrain(int logicDelete) {
        return this.baseMapper.queryAllTrain(logicDelete);
    }

    /**
     * 查询所有列车类型
     * @param logicDelete
     * @return
     */
    @Override
    public List<String> queryAllTrainType(int logicDelete) {
      return  this.baseMapper.queryAllTrainType(logicDelete);
    }
}
